#include <QCoreApplication>
#include <QTime>
#include <opencv2/opencv.hpp>
#include <cxfbfunction.h>
#include <string.h>

#include <fb.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <QThread>
#define IMAGE_SIZE (1024*768*4)

uchar *framebuffer  ;
uint display_fd     ;

class MyThread : public QThread
{
public:
    static   void m_sleep(unsigned long s)
    {
        msleep(s);
    }
};

void        Main_Loop()
{
    struct fb_var_screeninfo varinfo;
    struct fb_fix_screeninfo fixinfo;
    int ret;
    int count=0;

    ioctl(display_fd,FBIOGET_FSCREENINFO,&fixinfo);

    while(1)
    {
        if(count==580)
            count=0;
        ret = ioctl(display_fd, FBIOGET_VSCREENINFO, &varinfo);
        varinfo.yoffset=6 *(count++);
        ret = ioctl(display_fd, FBIOPUT_VSCREENINFO, &varinfo);
        MyThread::m_sleep(18);
    }
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    display_fd = initFrameBuffer("/dev/fb1",framebuffer,1024,768);

    cv::Mat image[5];

    image[0]=cv::imread("/opt/Test/Image/1.jpg");
    image[1]=cv::imread("/opt/Test/Image/2.jpg");
    image[2]=cv::imread("/opt/Test/Image/3.jpg");
    image[3]=cv::imread("/opt/Test/Image/4.jpg");
    image[4]=cv::imread("/opt/Test/Image/5.jpg");

    uint  *data=new uint [1024*768*5];
    int     cy=0;

    for(int i=0;i<5;++i)
    {
        for(int y=0;y< 768;++y)
        {
               for(int x=0;x<1024;++x)
               {
                   data[(y+768*i)*1024 + x] &=0;
                   data[(y+768*i)*1024 + x] |= image[i].data[y*image[i].step+x*3+2];
                   (data[(y+768*i)*1024 + x] <<= 8 ) |=image[i].data[y*image[i].step+x*3+1];
                   (data[(y+768*i)*1024 + x] <<= 8 ) |=image[i].data[y*image[i].step+x*3];
               }
           }
       }

    memcpy(framebuffer,data,IMAGE_SIZE*5);

    std::cout<<"All copying OK..."<<std::endl;

    Main_Loop();

    return a.exec();
}
